package vn.conghuy.talkwithme;

import java.util.ArrayList;

import android.content.Context;
import android.text.Html;
import android.text.Layout;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.webkit.WebView;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

public class CustomAdapter extends BaseAdapter {
	
	static final int TYPE_PLAIN_TEXT = 0;
	static final int TYPE_WEB = 1;
	
	ArrayList<String> data;
	LayoutInflater inflater;
	Context context;
		
	public CustomAdapter(Context context) {
		this.context = context;
		data = new ArrayList<String>();
		inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}
	
	public void addItem(String item){
		data.add(item);
		notifyDataSetChanged();
	}

	public int getCount() {
		return data.size();
	}

	public Object getItem(int position) {
		return data.get(position);
	}
	
	public String getItemString(int position){
		return getItem(position).toString();
	}

	public long getItemId(int position) {
		return position;
	}
	
	@Override
	public int getItemViewType(int position){
		String item = getItem(position).toString();
		if(item.contains("<img src=") && item.contains("</img>")){
			return TYPE_WEB;
		}
		else return TYPE_PLAIN_TEXT;
		
	}
	
	@Override
	public int getViewTypeCount() {
		return 2;
	}
	
	private boolean isYou(String item){
		if(item.contains("<b>"+ context.getString(R.string.you) + "</b>")){
			return true;
		}
		else return false;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		int type = getItemViewType(position);
		String item = getItemString(position);
		Holder holder;
		if(convertView == null){
			holder = new Holder();
			switch (type) {
			case TYPE_PLAIN_TEXT:
				convertView = inflater.inflate(R.layout.plaintext_item, null);
				holder.textView = (TextView)convertView.findViewById(R.id.t_plaintext);
				break;
			case TYPE_WEB:
				convertView = inflater.inflate(R.layout.web_item, null);
				holder.webView = (WebView)convertView.findViewById(R.id.w_webcontent);
				break;
			default:
				break;
			}
			convertView.setTag(holder);
		}
		else{
			holder = (Holder)convertView.getTag();
		}
		
		switch (type) {
		case TYPE_PLAIN_TEXT:
			holder.textView.setText(Html.fromHtml(item));
			if(isYou(item)){
				LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,0);
				params.gravity = Gravity.RIGHT;
				params.leftMargin = 30;
				params.rightMargin = 10;
				holder.textView.setLayoutParams(params);
				holder.textView.setBackgroundResource(R.drawable.bubble_green);
			}
			else {
				LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,0);
				holder.textView.setLayoutParams(params);
				params.gravity = Gravity.LEFT;
				params.leftMargin = 10;
				params.rightMargin = 30;
				holder.textView.setBackgroundResource(R.drawable.bubble_yellow);
			}
			break;
			
		case TYPE_WEB:
			holder.webView.loadData(item, "text/html", "utf-8");
			break;

		default:
			break;
		}
		return convertView;
	}
	
	class Holder{
		public TextView textView;
		public WebView webView;
	}
}
